package benny.ApkposStore.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import benny.ApkposStore.model.OrderPay;

public class OrderTablePay {
	/* table name */
	public static final String TABLENAME = "apkpos_orders_pay";
	/* column names */
	static final public String CID = "_id"; // AUTO generate
	static final public String CTS = "cts"; // timestamp

	static final public String CORDERID = "orderid";
	static final public String PAY_TYPEID = "pay_typeid"; // payment_name
	static final public String PAY_CHARGE = "pay_charge"; // amount
	static final public String RATE = "rate"; // exchange_rate
	static final public String ORIGINAL_AMOUNT = "original_amount"; // original_cash_amount

	private Context mCtx;

	public static final String createTableSQL = "create table if not exists "
			+ TABLENAME + "(" + CID + " integer primary key autoincrement,"
			+ CTS + " int not null default 0," + CORDERID
			+ " varchar(40) not null default 0," + PAY_TYPEID
			+ " varchar(10) ," + PAY_CHARGE + " int," + ORIGINAL_AMOUNT
			+ " int ," + RATE + " int)";

	private static String tag = "apkpos_orders_pay";

	public OrderTablePay(Context ctx) {
		mCtx = ctx;
	}

	/**
	 * insert a row
	 * 
	 * @param bean
	 *            :
	 * @param context
	 *            :Application's context
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insert(OrderPay bean) {

		ContentValues values = beanToContentValues(bean);
		values.remove(CID);

		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			long rowId = db.insertOrThrow(TABLENAME, null, values);
			Log.d(tag, "insert a row success");
			return rowId;
		} catch (SQLiteException e) {
			Log.e(tag, "insert " + TABLENAME + " error:" + e.getMessage());
		}

		return -1;
	}

	/**
	 * get a table cursor
	 * 
	 * @param ctx
	 *            :Application's context
	 * @return cursor
	 */
	public Cursor getCurSor(String selection, String selectionArgs[]) {
		String columns[] = { CID, CTS, CORDERID, PAY_TYPEID, PAY_CHARGE,
				ORIGINAL_AMOUNT, RATE };
		// String selection = CORDERID +"= ?";
		// String selectionArgs[] = {"100"};
		String groupBy = "";
		String having = "";
		String orderBy = "";
		String limit = "";

		Cursor cursor = null;
		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			cursor = db.query(TABLENAME, columns, selection, selectionArgs,
					groupBy, having, orderBy, limit);
			return cursor;
		} catch (SQLiteException e) {
			Log.e(tag, "query failed:" + e.getMessage());
		}
		return null;
	}

	/**
	 * update a service record
	 * 
	 * @param bean
	 */
	public boolean update(OrderPay bean) {
		try {
			String whereClause = " _id= " + bean.getId();
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			ContentValues values = beanToContentValues(bean);
			db.update(TABLENAME, values, whereClause, null);
			Log.d(tag, "update a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "update " + TABLENAME + " error:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * delete a row
	 * 
	 * @param rowId
	 * @param ctx
	 * @return
	 */
	public boolean delete(int rowId) {
		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			db.execSQL("delete from " + TABLENAME + " where _id = " + rowId);
			Log.d(tag, "delete a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "delete failed:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 按订单号删除
	 * 
	 * @param orderid
	 * @return
	 */
	public boolean deleteByOrderid(String orderid) {
		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx);
			db.execSQL("delete from " + TABLENAME + " where " + CORDERID
					+ " = " + orderid);
			Log.d(tag, "delete a row success");
		} catch (SQLiteException e) {
			Log.e(tag, "delete failed:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * convert db cursor to bean structure
	 * 
	 * @param cursor
	 * @return Order
	 */

	public OrderPay CurSorToGasBean(Cursor cursor) {
		OrderPay bean = new OrderPay();
		bean.setId(cursor.getInt(cursor.getColumnIndex(CID)));
		bean.setTs(cursor.getInt(cursor.getColumnIndex(CTS)));
		bean.setOrderid(cursor.getString(cursor.getColumnIndex(CORDERID)));
		bean.setPay_typeid(cursor.getInt(cursor.getColumnIndex(PAY_TYPEID)));
		bean.setPay_charge(cursor.getInt(cursor.getColumnIndex(PAY_CHARGE)));
		bean.setRate(cursor.getInt(cursor.getColumnIndex(RATE)));
		bean.setOriginal_amount(cursor.getInt(cursor
				.getColumnIndex(ORIGINAL_AMOUNT)));

		return bean;
	}

	private ContentValues beanToContentValues(OrderPay bean) {
		ContentValues values = new ContentValues();

		values.put(CID, bean.getId());
		values.put(CTS, bean.getTs());
		values.put(CORDERID, bean.getOrderid());
		values.put(PAY_TYPEID, bean.getPay_typeid());
		values.put(PAY_CHARGE, bean.getPay_charge());
		values.put(RATE, bean.getRate());
		values.put(ORIGINAL_AMOUNT, bean.getOriginal_amount());

		return values;
	}

}
